package JVM;


import com.sun.org.apache.bcel.internal.generic.NEW;
import org.junit.Test;

import java.sql.Time;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.concurrent.TimeUnit;

/**
 * @author:谢君臣
 * @Date:2021/4/811:09
 * @version:1.0
 * @function:
 */
class Java8锁 {

    public static void main(String[] args) {

    }
    @Test
    public void test1(){
        South south = new South();
        new Thread(() -> south.run1(),"A线程").start();
        new Thread(() -> south.run2(),"B线程").start();
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }  //
    @Test
    public void test2(){
        South south1 = new South();
        South south2 = new South();
        new Thread(() -> south1.run1(),"A线程").start();
        new Thread(() -> south2.run2(),"B线程").start();
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test3(){
        South south = new South();
        new Thread(() -> south.run1(),"A线程").start();
        new Thread(() -> south.run3(),"B线程").start();
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test4(){
        South south1 = new South();
        South south2 = new South();
        South south3 = new South();
        System.out.println(Thread.currentThread().getName()+ "执行了！执行时间:" +System.currentTimeMillis());
        new Thread(() -> south1.run4(),"A线程").start();
        new Thread(() -> south2.run3(),"B线程").start();
        new Thread(() -> south3.run5(),"c线程").start();
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test5(){
        South south1 = new South();
        System.out.println(Thread.currentThread().getName()+ "执行了！执行时间:" +System.currentTimeMillis());
        new Thread(() -> south1.run4(),"A线程").start();
        new Thread(() -> south1.run2(),"B线程").start();
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
class South{
    // synchronized 锁的对象是方法的调用者！、
    // 两个方法用的是同一个锁，谁先拿到谁执行！
    public synchronized void run1(){
        System.out.println(Thread.currentThread().getName()+"执行了！执行时间:" + System.currentTimeMillis() );
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"执行了！执行时间:" + System.currentTimeMillis() );
    }
    public synchronized void run2(){
        System.out.println(Thread.currentThread().getName()+"执行了！执行时间:" + System.currentTimeMillis() );
    }
    public void run3(){
        System.out.println(Thread.currentThread().getName()+"执行了！执行时间:" + System.currentTimeMillis() );
    }
    public static synchronized void run4(){
        System.out.println(Thread.currentThread().getName() + "成功创建！执行时间:" + System.currentTimeMillis() );
        try {
            TimeUnit.SECONDS.sleep(4);
            System.out.println(Thread.currentThread().getName() + "线程暂停4秒！");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("静态syn的方法被"+ Thread.currentThread().getName()+"执行了！执行时间:" + System.currentTimeMillis() );
    }
    public static synchronized void run5(){
        System.out.println("静态syn的方法被"+ Thread.currentThread().getName()+"执行了！执行时间:" + System.currentTimeMillis() );
    }
}